package ControllerSrevlet;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import MYJDBC.jdbc;

/**
 * Servlet implementation class AddVideo
 */
@WebServlet("/AddVideo")
public class AddVideo extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddVideo() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String str[] = new String[10];
		
		String name = null;
		
		int i=0;
		if(ServletFileUpload.isMultipartContent(request)){
	            FileItemFactory factory = new DiskFileItemFactory();
	            ServletFileUpload servletFileUpload = new ServletFileUpload(factory);
	            try {
	                List<FileItem> list = servletFileUpload.parseRequest(request);
	                for(FileItem item : list){
	                    if(item.isFormField()){
	                    	str[i]=item.getString("UTF-8");
	                    	i++;
	                    }else {
	                        str[i] = item.getName();
	                        if(item.getSize()!=0) {
	                        	item.write(new File("E:\\毕业设计\\zwmwork\\Music_web\\src\\main\\webapp\\Video\\"+item.getName()));
	                        }
	                        i++;
	                    }
	                }
	            } catch (FileUploadException e) {
	                e.printStackTrace();
	            } catch (Exception e) {
	                e.printStackTrace();
	            }
	        }
		
		jdbc jc = new jdbc();
		try {
			jc.SqlUpdate("insert into video_table (videoname,videotext,videoimg,videosrc,videopint,videouser,user_id,videopf,videoaudit) value ('"+str[0]+"','"+str[1]+"','"+str[2]+"','"+str[3]+"','"+str[4]+"','"+str[5]+"','"+str[6]+"',9,1);");
			jc.UpdateClose();
			response.sendRedirect("BlueVideoUserMyVideo");
		} catch (SQLException e) {
			
		}
		
		
		
	}

}
